<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProjects extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (Schema::hasTable('project')) {
            return;
        }

        Schema::create('project', function (Blueprint $table) {
            $table->id();
            $table->string('project_number')->default('')->comment('项目编号');
            $table->string('name')->default('')->comment('项目名称');
            $table->unsignedInteger('customer_id')->default(0)->comment('客户ID');
            $table->unsignedInteger('user_id')->default(0)->comment('负责人ID');
            $table->unsignedInteger('province_id')->default(0)->comment('省份ID');
            $table->string('province_name')->default('')->comment('省份名称');
            $table->unsignedInteger('city_id')->default(0)->comment('城市ID');
            $table->string('city_name')->default('')->comment('城市名称');
            $table->unsignedInteger('county_id')->default(0)->comment('区县ID');
            $table->string('county_name')->default('')->comment('区县名称');
            $table->string('address')->default('')->comment('详细地址');
            $table->string('plan_start_at')->default('')->comment('计划开始时间');
            $table->string('plan_complete_at')->default('')->comment('计划完成时间');
            $table->float('plan_work_hour')->default(0)->comment('计划工时');
            $table->string('status')->default('')->comment('状态');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        Schema::create('project_history', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('project_id')->default(0)->comment('项目ID');
            $table->string('status')->default('')->comment('状态(未开始: un_start, 进行中(已开始): started, 已关闭: closed, 已完成: completed, 已归档: filing)');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        Schema::create('task', function (Blueprint $table) {
            $table->id();
            $table->string('task_number')->default('')->comment('任务编号');
            $table->string('name')->default('')->comment('任务名称');
            $table->unsignedInteger('project_id')->default(0)->comment('所属项目ID');
            $table->string('type')->default('')->comment('任务类型');
            $table->string('plan_start_at')->default('')->comment('计划开始时间');
            $table->string('plan_complete_at')->default('')->comment('计划完成时间');
            $table->float('percent')->default(0)->comment('任务占比');
            $table->float('plan_work_hour')->default(0)->comment('计划工时');
            $table->float('work_hour')->default(0)->comment('实际工时');
            $table->string('description')->default('')->comment('任务说明');
            $table->string('status')->default('')->comment('状态');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        Schema::create('task_history', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('task_id')->default(0)->comment('任务ID');
            $table->string('status')->default('')->comment('状态(未开始: un_start, 进行中(已开始): started, 已完成: completed, 已暂停: paused)');
            $table->integer('create_time')->nullable();
            $table->integer('update_time')->nullable();
        });

        Schema::create('task_user', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('task_id')->default(0)->comment('任务ID');
            $table->unsignedInteger('user_id')->default(0)->comment('负责人ID');
        });

        Schema::create('task_product', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('task_id')->default(0)->comment('任务ID');
            $table->unsignedInteger('product_id')->default(0)->comment('设备ID');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('projects');
    }
}
